package com.renge.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.renge.eduservice.entity.EduCourse;
import com.renge.eduservice.entity.EduTeacher;
import com.renge.eduservice.entity.query.TeacherQuery;
import com.renge.eduservice.mapper.EduTeacherMapper;
import com.renge.eduservice.service.EduCourseService;
import com.renge.eduservice.service.EduTeacherService;
import com.renge.exception.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-05-16
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {
    @Autowired
    private EduCourseService eduCourseService;
    //根据条件分页查询
    @Override
    public void pageQuery(QueryWrapper<EduTeacher> wrapper, TeacherQuery teacherQuery) {
        if(!StringUtils.isEmpty(teacherQuery.getName())){
            wrapper.like("name",teacherQuery.getName());
        }if (!StringUtils.isEmpty(teacherQuery.getLevel())){
            wrapper.eq("level",teacherQuery.getLevel());
        }if(!StringUtils.isEmpty(teacherQuery.getBegin())){
            wrapper.ge("gmt_create",teacherQuery.getBegin());
        }if(!StringUtils.isEmpty(teacherQuery.getBegin())){
            wrapper.le("gmt_create",teacherQuery.getBegin());
        }
    }
    //首页查询教师4个
    @Override
    public List<EduTeacher> Indexfindteacher() {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("sort");
        wrapper.last("LIMIT 4");
        List<EduTeacher> list = this.list(wrapper);
        return list;
    }
    //前端首页讲师分页查询
    @Override
    public Map<String, Object> indexFindAllByPage(Integer current1, Integer pagesize) {
        //封装page
        Page<EduTeacher> page = new Page<>(current1,pagesize);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("sort");
        //得到数据
        final IPage<EduTeacher> ipage = this.page(page, wrapper);
        List<EduTeacher> records = page.getRecords();
        long total = page.getTotal();
        long size = page.getSize();
        long current = page.getCurrent();
        long pages = page.getPages();
        boolean previous = page.hasPrevious();
        boolean hasNext = page.hasNext();
        //填装数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("records",records);
        map.put("total",total);
        map.put("size",size);
        map.put("current",current);
        map.put("pages",pages);
        map.put("previous",previous);
        map.put("hasNext",hasNext);
        return map;
    }
    //根据讲师id获得讲师基本信息和其下课程
    @Override
    public Map<String, Object> findTeacherById(String teacherId) {
        //获得讲师基本信息
        EduTeacher teacher = this.getById(teacherId);
        if(StringUtils.isEmpty(teacher)){
            throw new GuliException(20001,"查询的讲师不存在");
        }
        //查询课程
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_id",teacherId);
        List<EduCourse> courseList = eduCourseService.list(wrapper);
        //完成封装
        HashMap<String, Object> map = new HashMap<>();
        map.put("teacher",teacher);
        map.put("courseList",courseList);
        return map;
    }
}
